package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustomException;
import com.heima.model.admin.dtos.AdSensitiveDto;
import com.heima.model.admin.dtos.AdSensitiveListDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.pojos.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * @author Franky
 * @version 1.0
 * @project heima-leadnews-shizhan
 * @description
 * @date 2023/9/17 10:12:27
 */
@Service
@Transactional
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    @Override
    public ResponseResult del(Integer id) {
        //直接调用mybatis删除方法
        if (removeById(id)){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }

    @Override
    public ResponseResult queryList(AdSensitiveListDto sensitiveListDto) {
        //分页查询
        Page ipage = new Page(sensitiveListDto.getPage(),sensitiveListDto.getSize());

        //按时间倒叙
        LambdaQueryWrapper<WmSensitive> queryWrapper = Wrappers.<WmSensitive>lambdaQuery()
                .orderByDesc(WmSensitive::getCreatedTime);
        if (StringUtils.isNoneBlank(sensitiveListDto.getName())){
            queryWrapper = queryWrapper.like(WmSensitive::getSensitives, sensitiveListDto.getName());
        }
        page(ipage,queryWrapper);

        //封装查询结果返回
        PageResponseResult pageResponseResult = new PageResponseResult((int) ipage.getCurrent(), (int) ipage.getSize(), (int) ipage.getTotal());
        pageResponseResult.setData(ipage.getRecords());
        pageResponseResult.setCode(AppHttpCodeEnum.SUCCESS.getCode());
        return pageResponseResult;
    }

    @Override
    public ResponseResult add(AdSensitiveDto sensitiveDto) {
        //如果内容不为空
        if (sensitiveDto.getSensitives() != null){
            WmSensitive wmSensitive = getOne(Wrappers.<WmSensitive>lambdaQuery().eq(WmSensitive::getSensitives,sensitiveDto.getSensitives()));
            if (Objects.nonNull(wmSensitive)){
                throw new CustomException(AppHttpCodeEnum.INFO_IS_EXIST);
            }else {
                WmSensitive wmSensitive1 = new WmSensitive();
                wmSensitive1.setSensitives(sensitiveDto.getSensitives());
                wmSensitive1.setCreatedTime(new Date());
                save(wmSensitive1);
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            }
        }

        return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
    }

    @Override
    public ResponseResult updateSensitive(AdSensitiveDto sensitiveDto) {
        WmSensitive wmSensitive = new WmSensitive();
        BeanUtils.copyProperties(sensitiveDto,wmSensitive);
        wmSensitive.setCreatedTime(new Date());
        updateById(wmSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
